//
//  WmcCertificationLabel.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/5/16.
//

import UIKit

class WmcCertificationView: UIView {
    // Lifecycle

    override init(frame: CGRect) {
        super.init(frame: frame)
        config()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    /// 认证视图
    lazy var certificationImageView: UIImageView = {
        let imageView = UIImageView(image: "home_wm_certification".wmc_image)
        imageView.contentMode = .scaleAspectFit
        imageView.isHidden = true
        setShadow(imageView)
        return imageView
    }()

    /// 认证文案
    lazy var certificationLabel: UILabel = {
        let label = UILabel.YP.Create(font: 12.yp.uiFont(weight: .medium),
                                      color: UIColor.white,
                                      alignment: .left)
        label.adjustsFontSizeToFitWidth = true
        setShadow(label)
        return label
    }()

    var staticAddressModel: WmcSelectAddressModel?

    var styleMode: WmcBaseWatermarkTemplateView.StyleMode = .dynamicDraw

    // 设置文案
    var isOpenTime: Bool = false
    var isOpenLocat: Bool = false

    var timeState: BehaviorRelay<WmcDateManager.TimeState> = .init(value: .notReal)
    var locateState: BehaviorRelay<WmcAmapLocationManager.Status> = .init(value: .fetching)
    var poiState: BehaviorRelay<WmcLocationManager.PoiStatus> = .init(value: .updatingPoi)

    func config() {
        addSubview(certificationImageView)
        certificationImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(8)
            make.size.equalTo(CGSize(width: 12, height: 13))
        }

        addSubview(certificationLabel)
        certificationLabel.snp.makeConstraints { make in
            make.top.right.bottom.equalToSuperview()
            make.left.equalTo(certificationImageView.snp.right).offset(4)
        }

        // 监听时间
        WmcDateManager.shared.timeState.subscribe(onNext: { [weak self] state in
            guard let weakSelf = self else { return }
            weakSelf.timeState.accept(state)
            weakSelf.showText()
        }).disposed(by: rx.disposeBag)

        // 监听定位
        WmcLocationManager.shared.locationStatus.subscribe(onNext: { [weak self] model in
            guard let weakSelf = self,
                  let model = model else { return }
            weakSelf.locateState.accept(model.status)
            weakSelf.showText()
        }).disposed(by: rx.disposeBag)

        // 监听poi
        WmcLocationManager.shared.poiStatus.subscribe(onNext: { [weak self] model in
            guard let weakSelf = self,
                  let model = model else { return }
            weakSelf.poiState.accept(model.status)
            weakSelf.showText()
        }).disposed(by: rx.disposeBag)
    }

    func showText() {
        let locationStatus = WmcAuthorizedManager.location()

        var isVerifiedPosition: Bool = false
        var addressModel: WmcSelectAddressModel?
        if styleMode.isStatic { // 静态
            addressModel = staticAddressModel
        }else{ // 动态
            addressModel = WmcLocationManager.shared.poiStatus.value?.data
        }
        if let addressModel = addressModel {
            isVerifiedPosition = addressModel.style == .normal || addressModel.style == .commonly
        }

        WmcAuthorizedManager.network { networkStatus in // 网络
            YPGCD.Main { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.certificationImageView.isHidden = true
                weakSelf.certificationLabel.isHidden = true
                var text = ""
                if weakSelf.timeState.value == .real,
                   weakSelf.isOpenTime == true,
                   weakSelf.locateState.value == .completion,
                   weakSelf.poiState.value == .updatePoiCompletion,
                   weakSelf.isOpenLocat == true,
                   locationStatus.info.isAvailable,
                   networkStatus.info.isAvailable,
                   isVerifiedPosition == true
                {
                    text = "鱼泡水印相机已验证时间和地点真实性"

                } else if weakSelf.timeState.value == .real,
                          weakSelf.isOpenTime,
                          networkStatus.info.isAvailable
                {
                    text = "鱼泡水印相机已验证时间真实性"

                } else if weakSelf.locateState.value == .completion,
                          weakSelf.poiState.value == .updatePoiCompletion,
                          weakSelf.isOpenLocat,
                          locationStatus.info.isAvailable,
                          networkStatus.info.isAvailable,
                          isVerifiedPosition
                {
                    text = "鱼泡水印相机已验证地点真实性"
                }

                if !text.isEmpty {
                    weakSelf.certificationLabel.isHidden = false
                    weakSelf.certificationImageView.isHidden = false
                    weakSelf.certificationLabel.text = text
                }
            }
        }
    }

    /// 设置视图阴影
    func setShadow(_ view: UIView, color: UIColor = .wmc_grayH5_606066) {
        view.layer.shadowColor = color.cgColor
        view.layer.shadowRadius = 4
        view.layer.shadowOffset = CGSize(width: 0, height: 1)
        view.layer.shadowOpacity = 0.8
    }
}
